package com.dbq.share;

import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.widget.Toast;

import com.tencent.connect.share.QQShare;
import com.tencent.connect.share.QzoneShare;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.Tencent;
import com.tencent.tauth.UiError;

import java.util.ArrayList;

import static com.dbq.share.YibaShareDelegateActivity.KEY_IS_QZONE;

/**
 * Created by sunkai on 2017/12/26.
 */

public class QQAssistActivity extends BaseAssistActivity {


    private boolean isQZone;
    private Tencent mTencent;
    private BaseUiListener listener;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        isQZone = getIntent().getExtras().getBoolean(KEY_IS_QZONE, false);

        mTencent = Tencent.createInstance(ShareUtil.QQ_APPID, getApplicationContext());
        listener = new BaseUiListener();
    }

    @Override
    protected void appNotInstallTips() {
        Toast.makeText(getApplicationContext(), R.string.dbq_socializ_qq_not_install, Toast.LENGTH_SHORT).show();
    }

    @Override
    protected boolean appInstall() {
        return checkAppInstall("com.tencent.mobileqq", "4.1");
    }

    protected boolean checkAppInstall(String packageName, String miniVersion) {
        PackageManager packageManager = getPackageManager();
        PackageInfo packageInfo = null;
        try {
            packageInfo = packageManager.getPackageInfo(packageName, 0);
            return compareVersion(packageInfo.versionName, miniVersion) >= 0;
        } catch (PackageManager.NameNotFoundException var4) {
            return false;
        }
    }

    private int compareVersion(String versionName, String defaultValue) {
        if (versionName == null && defaultValue == null) {
            return 0;
        } else if (versionName != null && defaultValue == null) {
            return 1;
        } else if (versionName == null && defaultValue != null) {
            return -1;
        } else {
            String[] var2 = versionName.split("\\.");
            String[] var3 = defaultValue.split("\\.");
            try {
                int var4;
                for (var4 = 0; var4 < var2.length && var4 < var3.length; ++var4) {
                    int var5 = Integer.parseInt(var2[var4]);
                    int var6 = Integer.parseInt(var3[var4]);
                    if (var5 < var6) {
                        return -1;
                    }
                    if (var5 > var6) {
                        return 1;
                    }
                }
                return var2.length > var4 ? 1 : (var3.length > var4 ? -1 : 0);
            } catch (NumberFormatException e) {
                return versionName.compareTo(defaultValue);
            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        Tencent.onActivityResultData(requestCode, resultCode, data, listener);
    }

    @Override
    protected void sendMessage() {
        if (!isResume) {
            shareCancel();
            return;
        }
        startShare();
        if (isQZone) {
            Bundle params = new Bundle();
            params.putString(QzoneShare.SHARE_TO_QQ_TITLE, shareWebPage.title);//必填
            params.putString(QzoneShare.SHARE_TO_QQ_SUMMARY, shareWebPage.description);//选填
            params.putString(QzoneShare.SHARE_TO_QQ_TARGET_URL, shareWebPage.url);//必填
            ArrayList<String> imageList = new ArrayList<>();
            params.putStringArrayList(QzoneShare.SHARE_TO_QQ_IMAGE_URL, imageList);
            mTencent.shareToQzone(this, params, listener);
        } else {
            Bundle params = new Bundle();
            params.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE, QQShare.SHARE_TO_QQ_TYPE_DEFAULT);
            params.putString(QQShare.SHARE_TO_QQ_TITLE, shareWebPage.title);
            params.putString(QQShare.SHARE_TO_QQ_SUMMARY, shareWebPage.description);
            params.putString(QQShare.SHARE_TO_QQ_TARGET_URL, shareWebPage.url);
            mTencent.shareToQQ(this, params, listener);
        }
    }

    class BaseUiListener implements IUiListener {

        @Override
        public void onComplete(Object o) {
            shareSuccess();
            finish();
        }

        @Override
        public void onError(UiError e) {
            shareError(e.errorMessage);
            finish();
        }

        @Override
        public void onCancel() {
            shareCancel();
            finish();
        }
    }
}
